package game;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import Netzwerk.GServer;

public class Mod  extends BasicGameState{

	public String mouse = "no input yet";
	private int anzahlLeben = 1;
	private int maximaleGewinne = 1;
	private transient GServer server;
	private boolean online;
	private int playing;
	private File[] files;
	private int[] maxPlayer;
	private int selectedMap;
	private int selectedMod;
	private int time;
	private boolean single;
	private int[] comPlayer;
	private String IpAdresse;
	private transient Font font;

	private int size;

	//private int sizefield;
	private float[][] field, ground, player;
	private Image[] fields, grounds, players;


	public Mod(int state, GServer server, boolean online, String IpAdresse){
		this.server = server;
		this.online = online;
		this.single = false;
		this.IpAdresse = IpAdresse;
	}
	public Mod(int state, boolean online, int playing){
		this.online = online;
		this.playing = playing;
		this.single = false;
	}

	public Mod(int state, boolean single){
		this.online=false;
		this.single=single;
	}


	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

		File f = new File("maps/");
		files = f.listFiles();
		maxPlayer = new int[files.length];

		this.font =new TrueTypeFont (new java.awt.Font("Cooper Black",0,18),false );
		this.selectedMap=-1;
		this.selectedMod=0;
		this.time=2;

		String row;

		for(int i = 0; i<files.length; i++){

			try {
				BufferedReader br = new BufferedReader(new FileReader(files[i]));
				row = br.readLine();
				row = br.readLine();
				maxPlayer[i]=Integer.parseInt(row);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		this.size=0;

		//Field Graphics
		this.fields = new Image[]{new Image("/Graphics/field3.png"),new Image("/Graphics/leer.png"),new Image("/Graphics/field2.jpg"),
				new Image("/Graphics/field1.jpg"),new Image("/Graphics/field4.jpg"),new Image("/Graphics/field5.png"),
				new Image("/Graphics/field6.gif"),new Image("/Graphics/field7.png"),new Image("/Graphics/field8.png"),
				new Image("/Graphics/field9.png"),new Image("/Graphics/field10.png"),new Image("/Graphics/field11.gif")};

		//Ground Graphics
		this.grounds = new Image[]{new Image("/Graphics/leer.png"),new Image("/Graphics/ground1.gif"),new Image("/Graphics/ground2.gif"),
				new Image("/Graphics/ground3.jpg"),new Image("/Graphics/ground4.jpg"),new Image("/Graphics/ground5.jpg"),
				new Image("/Graphics/ground6.jpg"),new Image("/Graphics/ground7.jpg"),new Image("/Graphics/ground8.jpg")};

	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setFont(this.font);
		g.setColor(Color.white);

		g.drawString("Gespielter Modus:", 100, 370);
		if(selectedMod==1){
			g.setColor(Color.red);
		}
		g.drawString("Classic", 100, 400);
		g.setColor(Color.white);
		if(selectedMod==2){
			g.setColor(Color.red);
		}
		g.drawString("Revange", 100, 425);
		g.setColor(Color.white);
		if(selectedMod==3){
			g.setColor(Color.red);
		}
		g.drawString("Item", 100, 450);
		g.setColor(Color.white);
		if(selectedMod==4){
			g.setColor(Color.red);
		}
		g.drawString("Bomberwoman", 100, 475);
		g.setColor(Color.white);

		if(!single && false){
			if(selectedMod==5){
				g.setColor(Color.red);
			}
			g.drawString("Zombie", 100, 500);
			g.setColor(Color.white);
		}

		g.drawString(mouse, 100, 100);
		g.drawString("Anzahl Leben: ", 100, 150);
		g.drawString(new Integer(anzahlLeben).toString(), 300, 150);
		g.drawString("-", 290, 150);
		g.drawString("+", 320, 150);
		g.drawString("Maximale Gewinne: ", 100, 200);
		g.drawString(new Integer(maximaleGewinne).toString(), 300, 200);
		g.drawString("-", 290, 200);
		g.drawString("+", 320, 200);
		g.drawString("Zeit (Minuten): ", 100, 250);
		g.drawString(new Integer(time).toString(), 300, 250);
		g.drawString("-", 290, 250);
		g.drawString("+", 320, 250);

		g.drawString("Gespielte Map:",400, 140);
		for(int i=0;i<files.length;i++){
			if(this.selectedMap==i){
				g.setColor(Color.red);
			}
			g.drawString(files[i].getName(), 400, 170+i*25);
			g.setColor(Color.white);
		}


		if(this.field != null && this.ground != null){

			for(int i=0;i<this.ground.length;i++){
				for(int j=0;j<this.ground[i].length;j++){
					this.grounds[(int) this.ground[i][j]].draw(600+i*7, 170+j*7,7,7);
					fields[(int) this.field[i][j]].draw(600+i*7, 170+j*7,7,7);
				}

			}
		}

		if(this.single && this.selectedMap>=0){
			g.drawString("Player:",770, 140);
			g.drawString("Player 1   Spieler",770, 170);

			for(int i = 1; i<this.comPlayer.length; i++){
				g.setColor(Color.white);
				g.drawString("Player "+(i+1),770, 170+i*25);

				if(this.comPlayer[i]==0){
					g.setColor(Color.red);
				}
				g.drawString("Aus",863, 170+i*25);
				g.setColor(Color.white);

				if(this.comPlayer[i]==1){
					g.setColor(Color.red);
				}
				g.drawString("CPU",910, 170+i*25);
				g.setColor(Color.white);
			}
		}

		if(this.selectedMap>=0 && this.selectedMod>0){
			g.drawString("Starte Spiel", 500, 500);
		}

	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

		Input in = gc.getInput();

		int xpos = Mouse.getX();
		int ypos = Mouse.getY();
		mouse=xpos+", "+ypos;

		if((xpos>500 && xpos<610) && (ypos<Main.sizey-500 && ypos>Main.sizey-517) && in.isMousePressed(0)
				&& this.selectedMap>=0 && this.selectedMod>0){
			//starte Spiel
			if(this.online){
				try {
					sbg.addState(new Lobby(9, 1, anzahlLeben, maximaleGewinne, this.files[this.selectedMap], server, this.selectedMod, this.time, this, this.IpAdresse));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sbg.getState(9).init(gc, sbg);
				sbg.enterState(9);
			}else if(this.single){

				sbg.addState(new Multiplayer(2, this.anzahlLeben-1, this.maximaleGewinne, this.comPlayer.length, this.files[this.selectedMap],
						this.selectedMod, this.time, this.comPlayer));
				sbg.getState(2).init(gc, sbg);
				sbg.enterState(2);
				
			}
			else{
				sbg.addState(new Multiplayer(2, this.anzahlLeben-1, this.maximaleGewinne, this.playing, this.files[this.selectedMap],
						this.selectedMod, this.time));
				sbg.getState(2).init(gc, sbg);
				sbg.enterState(2);
			}
		}

		if((xpos>100 && xpos<165) && (ypos<Main.sizey-400 && ypos>Main.sizey-417) && in.isMousePressed(0)){
			//classic
			selectedMod=1;
		}
		else if((xpos>100 && xpos<164) && (ypos<Main.sizey-425 && ypos>Main.sizey-442) && in.isMousePressed(0)){
			//Revange
			selectedMod=2;
		}
		else if((xpos>100 && xpos<136) && (ypos<Main.sizey-450 && ypos>Main.sizey-467) && in.isMousePressed(0)){
			//item
			selectedMod=3;
		}
		else if((xpos>100 && xpos<202) && (ypos<Main.sizey-475 && ypos>Main.sizey-492) && in.isMousePressed(0)){
			//bomberwoman
			selectedMod=4;
		}
		else if((xpos>100 && xpos<155) && (ypos<Main.sizey-500 && ypos>Main.sizey-517) && in.isMousePressed(0) && !this.single){
			//zombie
			selectedMod=5;
		}
		else if((xpos>290 && xpos<300) && (ypos<Main.sizey-155 && ypos>Main.sizey-165) && in.isMousePressed(0)){
			//leben-
			if(anzahlLeben > 1){
				anzahlLeben = anzahlLeben-1;
			}
		}
		else if((xpos>317 && xpos<330) && (ypos<Main.sizey-155 && ypos>Main.sizey-165) && in.isMousePressed(0)){
			//leben+
			if(anzahlLeben < 99){
				anzahlLeben = anzahlLeben+1;
			}
		}
		else if((xpos>290 && xpos<300) && (ypos<Main.sizey-205 && ypos>Main.sizey-215) && in.isMousePressed(0)){
			//gewinne-
			if(maximaleGewinne > 1){
				maximaleGewinne = maximaleGewinne-1;
			}
		}
		else if((xpos>317 && xpos<330) && (ypos<Main.sizey-205 && ypos>Main.sizey-215) && in.isMousePressed(0)){
			//gewinne+
			if(maximaleGewinne < 99){
				maximaleGewinne = maximaleGewinne+1;
			}
		}
		else if((xpos>290 && xpos<300) && (ypos<Main.sizey-255 && ypos>Main.sizey-265) && in.isMousePressed(0)){
			//time-
			if(time > 2){
				time = time-1;
			}
		}
		else if((xpos>317 && xpos<330) && (ypos<Main.sizey-255 && ypos>Main.sizey-265) && in.isMousePressed(0)){
			//time+
			if(time < 10){
				time = time+1;
			}
		}

		else if((xpos>400 && xpos<600) && (ypos<Main.sizey-170 && ypos>Main.sizey-600) && in.isMousePressed(0)){

			for(int i = 0; i<files.length; i++){
				if((xpos>400 && xpos<600) && (ypos<game.Main.sizey-170-i*25 && ypos>game.Main.sizey-170-i*25-15) && in.isMouseButtonDown(0)){
					this.selectedMap=i;

					String row;
					String[] help;
					String[][] help2;

					try {
						BufferedReader br = new BufferedReader(new FileReader(files[i]));
						row = br.readLine();
						this.size=Integer.parseInt(row);
						this.field= new float[this.size][this.size];
						this.ground= new float[this.size][this.size];
						row = br.readLine();
						this.comPlayer=new int[Integer.parseInt(row)];
						help2=new String[Integer.parseInt(row)][2];
						row = br.readLine();


						for(int j =0;j<this.size;j++){
							row=br.readLine();
							help=row.split(",");
							for(int k=0;k<this.size;k++){
								this.field[k][j]=Integer.parseInt(help[k]);
							}
						}

						br.readLine();

						for(int j =0;j<this.size;j++){
							row=br.readLine();
							help=row.split(",");
							for(int k=0;k<this.size;k++){
								this.ground[k][j]=Integer.parseInt(help[k]);
							}
						}


					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		else if((xpos>860 && xpos<955) && (ypos<Main.sizey-195 && ypos>Main.sizey-295) && in.isMousePressed(0) && this.single && this.selectedMap>=0){
			for(int i=1; i<this.comPlayer.length; i++){

				if((xpos>860 && xpos<900) && (ypos<Main.sizey-173-i*25 && ypos>Main.sizey-173-i*25-15)){
					this.comPlayer[i]=0;
				}
				else if((xpos>907 && xpos<950) && (ypos<Main.sizey-173-i*25 && ypos>Main.sizey-173-i*25-15)){
					this.comPlayer[i]=1;
				}
			}
		}
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return 6;
	}

}